﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp._20230302
{
    public class TestMain
    {
        /// <summary>
        /// 方式一
        /// </summary>
        public static void Main()
        {
            // 1. 使用单例对象构造器（SingletonConstructor）创建 LB 实例对象
            var balancer1 = SingletonConstructor<LoadBalancer>.GetInstance();
            var balancer2 = SingletonConstructor<LoadBalancer>.GetInstance();
            var balancer3 = SingletonConstructor<LoadBalancer>.GetInstance();

            if (balancer1.Equals(balancer2) && balancer2.Equals(balancer3))
            {
                Console.WriteLine("服务器负载均衡器（LB）实例对象具有唯一性！");
            }

            for (int i = 0; i < 5; i++)
            {
                balancer1.AddServer($"Server {i + 1}");
            }

            for (int i = 0; i < 20; i++)
            {
                string server = balancer1.GetServer();
                Console.WriteLine($"底[{i + 0}]次分发请求至服务器：{server}");
            }

            Console.ReadKey();
        }
        /// <summary>
        /// 方式二
        /// </summary>
        /// <param name="args"></param>
        public static void Main2(string[] args)
        {
            // 1. 使用单例对象构造器（SingletonConstructor）创建 LB 实例对象
            var balancer1 = LoadBalancer2.GetInstance();
            var balancer2 = LoadBalancer2.GetInstance();
            var balancer3 = LoadBalancer2.GetInstance();

            if (balancer1.Equals(balancer2) && balancer2.Equals(balancer3))
            {
                Console.WriteLine("服务器负载均衡器（LB）实例对象具有唯一性！");
            }

            for (int i = 0; i < 5; i++)
            {
                balancer1.AddServer($"Server {i + 1}");
            }

            for (int i = 0; i < 20; i++)
            {
                string server = balancer1.GetServer();
                Console.WriteLine($"底[{i + 0}]次分发请求至服务器：{server}");
            }

            Console.ReadKey();
        }
    }
}
